﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character
{
    public CharacterData data;

    public enum RoundState
    {
        Start,Thinking,Casting,End
    }

    public RoundState roundState;
    public Team team;
    public float currentHp;
    public float currentSp;
    public float attackRate = 1f;
    public float defenceRate = 1f;
    public float speedRate = 1f;
    public bool actionable = true;
    public bool alive = true;
    public Skill castingSkill;


    public List<Buff> buffs = new List<Buff>();

    public Character(CharacterData characterData)
    {
        buffs.Clear();
        data = characterData;
        currentHp = data.maxHp;
        currentSp = data.maxSp;
    }

    public void Action()
    {
        if (roundState == RoundState.Start)
        {
            StartAction();

        }
        else if (roundState == RoundState.Thinking)
        {
            ThinkingAction();
        }
        else if(roundState == RoundState.Casting)
        {
            CastingAction();
        }
        else if (roundState == RoundState.End)
        {
            Debug.Log(data.characterName + attackRate);
            EndAction();
        }
    }

    void StartAction()
    {
        CheckBuff();
        roundState = RoundState.Thinking;
    }

    void ThinkingAction()
    {
        UsingSkill("Attack");
    }

    void CastingAction()
    {
        if (!TargetChooser.IsCloosed())
        {
            return;
        }
        else
        {
            castingSkill.target = TargetChooser.GetChoosedCharacter();
            castingSkill.Effect();
            TargetChooser.ReSet();
            roundState = RoundState.End;

        }
    }

    void EndAction()
    {
        actionable = false;
        FightSystem.NextActor();
        roundState = RoundState.Start;
    }

    void CheckBuff()
    {
        foreach (Buff buff in buffs)
        {
            if (buff.isWork)
            {
                buff.BuffEffect();

            }
            if (buff.duration <= 0 && buff.isWork)
            {
                buff.BuffEnd();
            }
        }
    }


    public void UsingSkill(string skillName)
    {
        castingSkill = null;
        castingSkill = data.skills.Find(delegate (Skill skill)
        {
            return skill.skillName.Equals(skillName);
        });
        if (castingSkill == null)
        {
            Debug.LogError("No such skill named " + skillName + " in character " + data.characterName);
            roundState = RoundState.End;
            return;
        }
        castingSkill.user = this;
        if (castingSkill.CastAble())
        {
            castingSkill.Cast();
            roundState = RoundState.Casting;
        }
    }

    public void GetBuff(Buff buff)
    {
        buff.owner = this;
        buffs.Add(buff);
        buff.BuffStart();
    }

    public Team getOpponentTeam()
    {
        if (team == FightSystem.playerTeam)
            return FightSystem.enemyTeam;
        return FightSystem.playerTeam;
    }
}
